<?php

Yii::import("system.web.widgets.captcha.CCaptchaAction");

class CaptchaAction extends CCaptchaAction {

    const RENDER_REFRESHED_GET_VAR = "render_refreshed";

    private function init() {
        if (isset(Yii::app()->params['captcha'])) {
            if (isset(Yii::app()->params['captcha']['min-length'])) {
                $this->minLength = Yii::app()->params['captcha']['min-length'];
            }if (isset(Yii::app()->params['captcha']['max-length'])) {
                $this->maxLength = Yii::app()->params['captcha']['max-length'];
            }if (isset(Yii::app()->params['captcha']['width'])) {
                $this->width = Yii::app()->params['captcha']['width'];
            }if (isset(Yii::app()->params['captcha']['height'])) {
                $this->height = Yii::app()->params['captcha']['height'];
            }if (isset(Yii::app()->params['captcha']['padding'])) {
                $this->padding = Yii::app()->params['captcha']['padding'];
            }if (isset(Yii::app()->params['captcha']['back-color'])) {
                $this->backColor = Yii::app()->params['captcha']['back-color'];
            }if (isset(Yii::app()->params['captcha']['fore-color'])) {
                $this->foreColor = Yii::app()->params['captcha']['fore-color'];
            }if (isset(Yii::app()->params['captcha']['transparent'])) {
                $this->transparent = Yii::app()->params['captcha']['transparent'];
            }
        }
    }

    public function run() {
        $this->init();
//        if (isset($_GET[self::REFRESH_GET_VAR])) {  // AJAX request for regenerating code
//            $code = $this->getVerifyCode(true);
//            echo CJSON::encode(array(
//                'hash1' => $this->generateValidationHash($code),
//                'hash2' => $this->generateValidationHash(strtolower($code)),
//                // we add a random 'v' parameter so that FireFox can refresh the image
//                // when src attribute of image tag is changed
//                'url' => $this->getController()->createUrl($this->getId(), array(
//                    'v' => uniqid()
//                )),
//            ));
//        } else if (isset($_GET[self::RENDER_REFRESHED_GET_VAR])) {
//            $this->renderImage($this->getVerifyCode(true));
//        }
//        else
//            $this->renderImage($this->getVerifyCode());
        $this->renderImage($this->getVerifyCode(true));
        Yii::app()->end();
    }

}